package com.bjpowernode.crm.settings.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bjpowernode.crm.commons.enums.RespCode;
import com.bjpowernode.crm.commons.resp.Result;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * ClassName:DicValueController
 * Package:com.bjpowernode.crm.settings.web.controller
 * Date:2022/6/3 17:51
 * Description:
 * Author:zsm@zhangshuming.com
 */
@Controller
public class DicValueController {

    /**
     * 业务逻辑接口
     * @return
     */
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private DicTypeService dicTypeService;


    @RequestMapping("/settings/dictionary/value/index.do")
    public String index(){
        return "settings/dictionary/value/index";
    }


    @RequestMapping("/settings/dictionary/value/createDicValuePage.do")
    public String createDicValuePage(HttpServletRequest request){
        List<DicType> dicTypes = dicTypeService.queryAllDicType();
        request.setAttribute("dicTypes", dicTypes);
        return "settings/dictionary/value/save";
    }

    @RequestMapping("/settings/dictionary/value/updateDicValuePage.do")
    public String updateDicValuePage(HttpServletRequest request,String id){
        // 根据id获取数据字典值信息
        DicValue dicValue = dicValueService.queryAllDicValueById(id);
        request.setAttribute("dicValue",dicValue);
        return "settings/dictionary/value/edit";
    }


    @RequestMapping("/settings/dictionary/type/queryAllDicValueList.do")
    @ResponseBody
    public Result queryAllDicValuePage(){
        Result result = Result.FRR();
        try {
            List<DicValue> dicValueList = dicValueService.queryAllDicValueList();
            if (dicValueList != null){
                result = Result.SUCCESS();
                result.setBody(dicValueList);
            }else {
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @RequestMapping("/settings/dictionary/type/saveCreateDicValue.do")
    @ResponseBody
    public Result saveCreateDicValue(DicValue dicValue){
        Result result = Result.FRR();
        try {
            // 完善对象的信息id
            dicValue.setId(UUIDUtils.getUUID());
            // 新增数据字典类型
            dicValueService.saveCreateDicValue(dicValue);
            result = Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }
    @RequestMapping("/settings/dictionary/value/saveEditDicValue.do")
    @ResponseBody
    public Result saveEditDicValue(DicValue dicValue){
        Result result = Result.FRR();
        try {
            // 更新数据字典值
            dicValueService.saveEditDicValue(dicValue);
            result = Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @RequestMapping("settings/dictionary/type/deleteDicValueById.do")
    @ResponseBody
    public Result deleteDicValueById(@RequestParam("id") String[] ids){
        Result result = Result.FRR();
        try {
            // 删除数据
            dicValueService.deleteDicValueById(ids);
            result = Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }
}
